package com.tadpole.music.presenter.me;

import com.tadpole.music.bean.me.ZhenTiStudentErrorBean;
import com.tadpole.music.callback.BaseCallback;
import com.tadpole.music.config.Constant;
import com.tadpole.music.config.UrlConfig;
import com.tadpole.music.iView.me.ZhenTiStudentErrorIView;
import com.tadpole.music.model.base.DataModel;
import com.tadpole.music.model.base.ModelToken;
import com.tadpole.music.presenter.base.BasePresenter;
import com.tadpole.music.utils.JsonParseUtil;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的学生的真题实战 错题列表
 * Created By 孙岩
 * on 2020/12/29
 */
public class ZhenTiStudentErrorPresenter extends BasePresenter<ZhenTiStudentErrorIView> {
    private List<ZhenTiStudentErrorBean.DataBeanX.DataBean> list;
    private int page = 2;

    @SuppressWarnings("unchecked")
    public void getZhenTiList(final boolean isRefresh, String user_id, String rank_id) {
        if (isViewAttached()) {
            String url = UrlConfig.getZhenTiStudentList + "user_id=" + user_id + "&rank_id=" + rank_id + "&page=1";
            String token = SpUtil.getInstance(getView().getContext()).getString(Constant.TOKEN, "");
            DataModel.request(ModelToken.GET_HEAD_REQUEST)
                    .url(url)
                    .token(token)
                    .execute(new BaseCallback<String>() {
                        @Override
                        public void onSuccess(String data) {
                            try {
                                JSONObject obj = new JSONObject(data);
                                if (obj.getInt("code") == 200) {
                                    JSONObject object = new JSONObject(obj.getString("data"));
                                    JSONArray array = new JSONArray(object.getString("data"));
                                    list = new ArrayList<>();
                                    for (int i = 0; i < array.length(); i++) {
                                        JSONObject jsonObject = new JSONObject(array.get(i).toString());
                                        ZhenTiStudentErrorBean.DataBeanX.DataBean dataBean = new ZhenTiStudentErrorBean.DataBeanX.DataBean(
                                                JsonParseUtil.getInt(jsonObject, "id"),
                                                JsonParseUtil.getStr(jsonObject, "created_at"),
                                                JsonParseUtil.getInt(jsonObject, "score"),
                                                JsonParseUtil.getInt(jsonObject, "err_num"),
                                                JsonParseUtil.getInt(jsonObject, "time")
                                        );
                                        list.add(dataBean);
                                    }

                                    if (isRefresh) {
                                        page = 2;
                                        getView().showList(list);
                                        getView().notifyAdapter();
                                    } else {
                                        getView().showList(list);
                                    }
                                } else {
                                    ToastUtils.show(obj.getString("msg"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(String msg) {
                            getView().showErr();
                            getView().stopRefresh();
                        }

                        @Override
                        public void onComplete() {
                            getView().stopRefresh();
                        }

                        @Override
                        public void onBefore() {

                        }
                    });
        }
    }

    @SuppressWarnings("unchecked")
    public void getZhenTiListMore( String user_id, String rank_id) {
        if (isViewAttached()) {
            String url = UrlConfig.getZhenTiStudentList + "user_id=" + user_id + "&rank_id=" + rank_id + "&page="+page;
            String token = SpUtil.getInstance(getView().getContext()).getString(Constant.TOKEN, "");
            DataModel.request(ModelToken.GET_HEAD_REQUEST)
                    .url(url)
                    .token(token)
                    .execute(new BaseCallback<String>() {
                        @Override
                        public void onSuccess(String data) {
                            try {
                                JSONObject obj = new JSONObject(data);
                                if (obj.getInt("code") == 200) {
                                    JSONObject object = new JSONObject(obj.getString("data"));
                                    JSONArray array = new JSONArray(object.getString("data"));
                                    List<ZhenTiStudentErrorBean.DataBeanX.DataBean> newList = new ArrayList<>();
                                    for (int i = 0; i < array.length(); i++) {
                                        JSONObject jsonObject = new JSONObject(array.get(i).toString());
                                        ZhenTiStudentErrorBean.DataBeanX.DataBean dataBean = new ZhenTiStudentErrorBean.DataBeanX.DataBean(
                                                JsonParseUtil.getInt(jsonObject, "id"),
                                                JsonParseUtil.getStr(jsonObject, "created_at"),
                                                JsonParseUtil.getInt(jsonObject, "score"),
                                                JsonParseUtil.getInt(jsonObject, "err_num"),
                                                JsonParseUtil.getInt(jsonObject, "time")
                                        );
                                        newList.add(dataBean);
                                    }
                                    list.addAll(newList);
                                    getView().notifyAdapter();
                                    page++;
                                }else if(obj.getInt("code")==201){

                                }
                                else {
                                    ToastUtils.show(obj.getString("msg"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(String msg) {
                            getView().showErr();
                            getView().stopRefresh();
                        }

                        @Override
                        public void onComplete() {
                            getView().stopRefresh();
                        }

                        @Override
                        public void onBefore() {

                        }
                    });
        }
    }
}
